#import "CommonUtil.h"


@implementation CommonUtil
+(NSString *) getWeekChinese:(int) week{
	switch (week) {
		case 1: return @"日"; break;
		case 2: return @"一"; break;
		case 3: return @"二"; break;
		case 4: return @"三"; break;
		case 5: return @"四"; break;
		case 6: return @"五"; break;
		case 7: return @"六"; break;
		default: return @""; break;
	}

}

+(NSString *) getCurrentDate{
	NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
	NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit;
	NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
	
	NSString *today = [[NSString alloc] initWithFormat:@"%d年%d月%d日 周%@", 
					  [comps year],[comps month],[comps day],[CommonUtil getWeekChinese:[comps weekday]]];
	
	//[comps release];
	//[calendar release];
	return today;
}


+(void) initProvinces:(NSMutableDictionary *)provinceDict {
	[provinceDict setObject:@"anhui" forKey:@"安徽"];
	[provinceDict setObject:@"beijing" forKey:@"北京"];
	[provinceDict setObject:@"chongqing" forKey:@"重庆"];
	[provinceDict setObject:@"fujian" forKey:@"福建"];
	[provinceDict setObject:@"gansu" forKey:@"甘肃"];
	
	[provinceDict setObject:@"guangdong" forKey:@"广东"];
	[provinceDict setObject:@"guangxi" forKey:@"广西"];
	[provinceDict setObject:@"guizhou" forKey:@"贵州"];
	[provinceDict setObject:@"hainan" forKey:@"海南"];
	[provinceDict setObject:@"hebei" forKey:@"河北"];
	
	[provinceDict setObject:@"heilongjiang" forKey:@"黑龙江"];
	[provinceDict setObject:@"heilongjiang" forKey:@"黑龙"];
	[provinceDict setObject:@"henan" forKey:@"河南"];
	[provinceDict setObject:@"hubei" forKey:@"湖北"];
	[provinceDict setObject:@"hunan" forKey:@"湖南"];
	[provinceDict setObject:@"jiangsu" forKey:@"江苏"];
	
	[provinceDict setObject:@"jiangxi" forKey:@"江西"];
	[provinceDict setObject:@"jilin" forKey:@"吉林"];
	[provinceDict setObject:@"liaoning" forKey:@"辽宁"];
	[provinceDict setObject:@"neimenggu" forKey:@"内蒙古"];
	[provinceDict setObject:@"neimenggu" forKey:@"内蒙"];
	[provinceDict setObject:@"ningxia" forKey:@"宁夏"];
	
	[provinceDict setObject:@"qinghai" forKey:@"青海"];
	[provinceDict setObject:@"shandong" forKey:@"山东"];
	[provinceDict setObject:@"shanghai" forKey:@"上海"];
	[provinceDict setObject:@"shanxi" forKey:@"山西"];
	[provinceDict setObject:@"shanxi2" forKey:@"陕西"];
	
	[provinceDict setObject:@"shenzhen" forKey:@"深圳"];
	[provinceDict setObject:@"sichuan" forKey:@"四川"];
	[provinceDict setObject:@"taiwan" forKey:@"台湾"];
	[provinceDict setObject:@"tianjin" forKey:@"天津"];
	[provinceDict setObject:@"xinjiang" forKey:@"新疆"];
	
	[provinceDict setObject:@"xizang" forKey:@"西藏"];
	[provinceDict setObject:@"yunnan" forKey:@"云南"];
	[provinceDict setObject:@"zhejiang" forKey:@"浙江"];
	[provinceDict setObject:@"" forKey:@""];
}


+(NSString *) getProvinceCode:(NSMutableDictionary *)provinceDict provinceName:(NSString *)pName {
	if( [provinceDict objectForKey:pName] == nil){
		return @"";
	}else {
		return [provinceDict objectForKey:pName];
	}
}

//获取前、后子串中间的字符串
+(NSString *)getSubstring:(NSString *)source prefix:(NSString *)p suffix:(NSString *)s {
	NSRange range1 = [source rangeOfString:p];
	NSRange range2 = [source rangeOfString:s options:NSBackwardsSearch];
	
	if(range1.length > 0 && range2.length > 0) {
		return [source substringWithRange:NSMakeRange(range1.location, range2.location - range1.location + [s length])];
	}else {
		return @"";
	}
}

//获取前、后子串中间的字符串
+(NSString *)getSubstringTrim:(NSString *)source prefix:(NSString *)p suffix:(NSString *)s {
	NSRange range1 = [source rangeOfString:p];
	NSRange range2 = [source rangeOfString:s options:NSBackwardsSearch];
	
	if(range1.length > 0 && range2.length > 0) {
		return [source substringWithRange:NSMakeRange(range1.location + [p length], range2.location - range1.location - [s length] + 1)];
	}else {
		return @"";
	}
}

//获取比当前值大的整值
+(int)getCeiling:(int)aNumber {
	int i = 0;
	while(aNumber / 10 > 0) {
		aNumber = aNumber / 10;
		i++;
	}
	
	int max = aNumber + 1;
	while(i > 0) {
		max = max * 10;
		i--;
	}
	return max;
}

+ (int)getMaxNumber:(NSArray *) arr {
	double max = 0;
	
	for(int i = 1; i < [arr count]; i++) {
		NSArray *item = [arr objectAtIndex:i];
		for (int j = 1; j < [item count]; j++) {
			if( [[item objectAtIndex:j] doubleValue] > max ) {
				max = [[item objectAtIndex:j] doubleValue];
			}
		}
	}
	return (int)max;
}

+ (BOOL) isConnected {  
    // Create zero addy  
    struct sockaddr_in zeroAddress;  
    bzero(&zeroAddress, sizeof(zeroAddress));  
    zeroAddress.sin_len = sizeof(zeroAddress);  
    zeroAddress.sin_family = AF_INET;  
    // Recover reachability flags  
    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);  
    SCNetworkReachabilityFlags flags;  
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);  
    CFRelease(defaultRouteReachability);  
    if (!didRetrieveFlags) {  
        NSLog(@"Error. Could not recover network reachability flags");  
        return NO;  
    }  
    BOOL isReachable = flags & kSCNetworkFlagsReachable;  
    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;  
    BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;  
    //NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com.cn/"];  
    NSURL *testURL = [NSURL URLWithString:@"http://app.cnxianzai.com:8080/reachable.html"];  
    NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL  cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5.0];  
    //NSURLConnection *testConnection = [[[NSURLConnection alloc] initWithRequest:testRequest delegate:nil] autorelease];  
    NSData *data = [NSURLConnection sendSynchronousRequest:testRequest
										 returningResponse:nil error:nil];
    //return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO; 
    return ((isReachable && !needsConnection) || nonWiFi) ? (data ? YES : NO) : NO;  
}  

+(UIImage*) imageFromPDF:(CGPDFDocumentRef)pdf withPageNumber:(NSUInteger)pageNumber withScale:(CGFloat)scale {
	if(pageNumber > 0 && pageNumber < CGPDFDocumentGetNumberOfPages(pdf)) {
		CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf,pageNumber);
		CGRect tmpRect = CGPDFPageGetBoxRect(pdfPage,kCGPDFMediaBox);
		CGRect rect = CGRectMake(tmpRect.origin.x,tmpRect.origin.y,tmpRect.size.width*scale,tmpRect.size.height*scale);
		UIGraphicsBeginImageContext(rect.size);
		CGContextRef context = UIGraphicsGetCurrentContext();
		CGContextTranslateCTM(context,0,rect.size.height);
		CGContextScaleCTM(context,scale,-scale);
		CGContextDrawPDFPage(context,pdfPage);
		UIImage* pdfImage = UIGraphicsGetImageFromCurrentImageContext();
		UIGraphicsEndImageContext();
		return pdfImage;
	}
	return nil;
}

+ (NSString *)applicationDocumentsDirectory {
	return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

/*
//解压文件
+ (NSString*)unZipData:(NSString *)zipFileName extName:(NSString *)extName
{
	BOOL isDir;
    NSFileManager *filemanager = [NSFileManager defaultManager];
	//NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:nil];
    NSString *filePath = [kDocumentFolder stringByAppendingPathComponent:zipFileName];
    NSLog(@"unZipData:%@, %@", filePath, [self applicationDocumentsDirectory]);
	if ([filemanager fileExistsAtPath:filePath isDirectory:&isDir] && !isDir) 
	{
		//解压文件
        NSString *forderPath = [self applicationDocumentsDirectory];
		ZipArchive* zipFile = [[ZipArchive alloc] init];
		[zipFile UnzipOpenFile:filePath Password:kZipPassword]; // B (the zip got password)
		[zipFile UnzipFileTo:forderPath overWrite:YES];
		[zipFile UnzipCloseFile];
		[zipFile release];
        
        NSString *fileName = [zipFileName stringByDeletingPathExtension];
        fileName = [NSString stringWithFormat:@"%@.pdf",fileName];
        NSString *newFilePath = [forderPath stringByAppendingPathComponent:fileName];
        return newFilePath;
	}
	else {
		NSLog(@"zip包文件不存在");
        return nil;
	}
}*/
@end
